package com.aisino.cvasp.application.service;

import com.aisino.cvasp.application.domain.Education;
import com.aisino.cvasp.application.domain.FormData;
import com.aisino.cvasp.application.util.DateUtils;
import com.aisino.cvasp.application.util.ObjectFieldOfNoNull;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfStamper;

import java.io.IOException;

public class CreateForm {

    public static void createForm(FormData formData, AcroFields acroFields, PdfStamper ps) throws DocumentException, IOException {


        BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        // 遍历所有的表单
        for (String key : acroFields.getFields().keySet()) {
            //可为每个表单单独设置样式
            System.out.println("这是表单的某一key："+key);
            acroFields.setFieldProperty(key, "textfont", base, null);
            acroFields.setFieldProperty(key, "textsize", new Float(8), null);
        }


        //为指定表单 赋值
        acroFields.setField("centerName",formData.getCenterName());
        acroFields.setField("applicantName",formData.getApplicantName());

        // 勾选
        acroFields.setField("piGender" + formData.getGender(), "On", true);
        // 字体自适应
        acroFields.setFieldProperty("piBirthCity", "textsize", addrLength(formData.getPiBirthCity(), "piBirthCity", acroFields, 6f), null);
        acroFields.setField("piBirthCity",formData.getPiBirthCity());

        //循环写表
        for (int j = 0; j < formData.getEducation().size(); j++) {
            Education education = formData.getEducation().get(j);
            ObjectFieldOfNoNull.noNullStringAttr(education);
            acroFields.setField("edBeginTime" + j, DateUtils.date2String(education.getEdBeginTime(), DateUtils.DATE_PATTERN.YYYY_MM_DD));
            acroFields.setField("edEndTime" + j, DateUtils.date2String(education.getEdEndTime(), DateUtils.DATE_PATTERN.YYYY_MM_DD));
            acroFields.setField("edSchool" + j, education.getEdSchoolName() + "/" + education.getEdSchoolAddress());
        }

    }


    //文本域字体缩放自适应
    public static float addrLength(String addr, String fileName, AcroFields form, float fontSize) throws IOException, DocumentException {
        BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        Rectangle position = form.getFieldPositions(fileName).get(0).position;
        float textBoxWidth = position.getWidth();
        /*文本框高度*/
        float textBoxHeight = position.getHeight();
        /*文本单行行高*/
        float ascent = baseFont.getFontDescriptor(baseFont.ASCENT, fontSize);
        /*baseFont渲染后的文字宽度*/
        float textWidth = baseFont.getWidthPoint(addr, fontSize);

        /*文字宽度大于文本框宽度，则缩小字体*/
        if (textWidth > textBoxWidth) {
            while (textWidth > textBoxWidth) {
                fontSize--;
                textWidth = baseFont.getWidthPoint(addr, fontSize);
            }
        }
        return fontSize;
    }
}
